package com.qboot.core.interceptor;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.qboot.common.utils.AssertUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * 预览模式拦截器
 *
 * @author Tellsea
 * @date 2023/2/3
 */
@Slf4j
public class PreviewInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("请求地址：" + request.getRequestURI());
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Method method = handlerMethod.getMethod();
            SaCheckPermission saCheckPermission = method.getAnnotation(SaCheckPermission.class);
            if (!ObjectUtils.isEmpty(saCheckPermission)) {
                String[] permissionList = saCheckPermission.value();
                for (String permission : permissionList) {
                    AssertUtils.isTrue(permission.contains(":save")
                            || permission.contains(":update")
                            || permission.contains(":remove")
                            || permission.contains(":import")
                            || permission.contains(":removeBatch")
                            || permission.contains(":export"), "演示环境，不能操作哟");
                }
            }
        }
        return true;
    }
}
